var core    = require("core")
,   crypto  = require('crypto')
,   utils   = core.utils
,   send    = core.send;

var encryption = function(req, next){
    if(!req.body.keys){
        var error = new Error(new send(901).msg);
        error.status = 901;
        return next(error);
    };
    if(!req.body.time){
        var error = new Error(new send(902).msg);
        error.status = 902;
        return next(error);
    };
    var body = {};
    for(var i in req.body)
        if(i !== "keys")
            body[i] = req.body[i];

    var body = utils.sort(body);
    sha1 = crypto.createHash('sha1');
    sha1.update(body);
    body = sha1.digest("hex");
    if(body === req.body.keys){
        body = {};
        for(var i in req.body)
            if(i !== "keys")
                body[i] = req.body[i];

        req.body = body;
        next();
    }else{
        var error = new Error(new send(901).msg);
        error.status = 901;
        next(error);
    }
};

module.exports = encryption;
